<template>
  <div class="api-settings">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>API设置</span>
          <el-button type="primary" @click="handleAddApi">添加API</el-button>
        </div>
      </template>
      <el-table :data="apis" style="width: 100%" empty-text="暂无数据">
        <el-table-column prop="name" label="API名称" />
        <el-table-column prop="url" label="URL" show-overflow-tooltip />
        <el-table-column prop="method" label="方法" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'info'">
              {{ scope.row.status === 'active' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form :model="apiForm" label-width="120px">
        <el-form-item label="API名称">
          <el-input v-model="apiForm.name" />
        </el-form-item>
        <el-form-item label="URL">
          <el-input v-model="apiForm.url" />
        </el-form-item>
        <el-form-item label="方法">
          <el-select v-model="apiForm.method" placeholder="请选择方法">
            <el-option label="GET" value="GET" />
            <el-option label="POST" value="POST" />
            <el-option label="PUT" value="PUT" />
            <el-option label="DELETE" value="DELETE" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="apiForm.status" :active-value="'active'" :inactive-value="'inactive'" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue';
import { useApiStore } from '@/store/api';

export default defineComponent({
  name: 'ApiSettings',
  setup() {
    const apiStore = useApiStore();
    const dialogVisible = ref(false);
    const dialogTitle = ref('添加API');
    const apis = ref([]);

    const apiForm = reactive({
      id: '',
      name: '',
      url: '',
      method: '',
      status: 'active' as 'active' | 'inactive'
    });

    const handleAddApi = () => {
      dialogTitle.value = '添加API';
      apiForm.id = '';
      apiForm.name = '';
      apiForm.url = '';
      apiForm.method = '';
      apiForm.status = 'active';
      dialogVisible.value = true;
    };

    const handleEdit = (row: any) => {
      dialogTitle.value = '编辑API';
      Object.assign(apiForm, row);
      dialogVisible.value = true;
    };

    const handleDelete = async (row: any) => {
      await apiStore.deleteApi(row.id);
      await fetchApis();
    };

    const handleSave = async () => {
      if (apiForm.id) {
        await apiStore.updateApi(apiForm);
      } else {
        await apiStore.createApi(apiForm);
      }
      dialogVisible.value = false;
      await fetchApis();
    };

    const fetchApis = async () => {
      //apis.value = await apiStore.fetchApis();
    };

    fetchApis();

    return {
      dialogVisible,
      dialogTitle,
      apis,
      apiForm,
      handleAddApi,
      handleEdit,
      handleDelete,
      handleSave
    };
  }
});
</script>

<style lang="scss" scoped>
.api-settings {
  padding: 20px;

  .box-card {
    margin-bottom: 20px;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style> 